const {Sequelize,Op,Model,DataTypes,QueryTypes} = require("sequelize");
global.appConfig = {
  session_id: 'dqk-ids-sess',
  // 扫描指定文件夹下的routers
  controllerUrl: './controller',
  modelUrl:'./model',
  // 项目router 权限根目录
  authPrefixUrl: '/dicms/api',
  staticPrefixUrl: '/dicms/static',
  //允许通过的url
  allowedUrls:[
    // '/favicon.ico',
    // '/phonemanage/*',
    // '/dicms/data/public/*',
    // '/dicms/static/desktop/data/public/*',
    // '/dicms/static/desktop/ui1/page/login.html',
    // '/dicms/static/desktop/ui1/page/404.html',
    // '/dicms/static/desktop/ui1/page/403.html',
    // '/dicms/static/desktop/ui1/my.html',
    // '/dicms/static/desktop/ui1/lib/layui-v2.5.5/font/iconfont.woff2?v=250',
    

    
    // // todo bug 暴露权限
    // '/dicms/static/desktop/ui1/api/*',
    // '/dicms/static/desktop/ui1/css/*',
    // '/dicms/static/desktop/ui1/images/*',
    // '/dicms/static/desktop/ui1/js/*',
    // '/dicms/static/desktop/ui1/lib/*',

    // //API放行url
    // '/dicms/api/desktop/sys_user/login',
    // '/dicms/api/desktop/sys_user/logout',

    // '/dqk-data-sys/sys_user/*',
    // '/zabbix/api/*',
  ],
  // require项目根目录
  rootUrl: __dirname,

  // db访问类型 Sequelize,Sqlite
  dbType:'Mysql',
  dbName:'dicms',
  dbUsername:'root',
  dbPassword:'root',
  dbUrl:'172.25.62.12',
  dbPort:'3306',
  dbPath:'./public/dicms/data/private/db/dicms-db.db',


  dbType2:'Sqlite',
  dbName2:'dicms',
  dbUsername2:'root',
  dbPassword2:'root',
  dbUrl2:'localhost',
  dbPort2:'3306',
  dbPath2:'./public/dicms/data/private/db/dqk_db.db',

  // 内存数据库
  db: {
    // 'admin': {
    //   username: 'admin', password: '123456',
    //   authUrls: ['/dicms/data/private/*']
    // }
  },

};

// 方法 2: 分别传递参数 (sqlite)
(async function init(){
  appConfig.rootUrl = appConfig.rootUrl.replace(/\\/g,"/");
  global.appRequire=function(url){
    return require(global.appConfig.rootUrl+"/"+url);
  };

  global.initAllowedUrls=function(urls){
    return urls.map(function(val){
      val = val.replace("*",".*");
      return new RegExp(`^${val}$`);
    })
  };

   //sequelize配置
  global.Sequelize = Sequelize;
  global.Op = Op;
  global.Model = Model;
  global.DataTypes = DataTypes;
  global.QueryTypes = QueryTypes;

  if(appConfig.dbType==="Sqlite"){
    global.sequelize = new Sequelize({
      dialect: appConfig.dbType.toLowerCase(),
      storage: appConfig.dbPath,
      logging: console.log,
      define:{
        raw:true
      }
    });
  }else if(appConfig.dbType==="Mysql"){
    global.sequelize = new Sequelize(appConfig.dbName,appConfig.dbUsername,appConfig.dbPassword,{
      dialect: appConfig.dbType.toLowerCase(),
      host: appConfig.dbUrl,
      port: appConfig.dbPort,
      logging: console.log,
      define:{
        raw:true
      }
    });
  }
  if(appConfig.dbType2==="Sqlite"){
    global.sequelize2 = new Sequelize({
      dialect: appConfig.dbType2.toLowerCase(),
      storage: appConfig.dbPath2,
      logging: console.log,
      define:{
        raw:true
      }
    });
  } else if(appConfig.dbType2==="Mysql"){
    global.sequelize2 = new Sequelize(appConfig.dbName2,appConfig.dbUsername2,appConfig.dbPassword2,{
      dialect: appConfig.dbType2.toLowerCase(),
      host: appConfig.dbUrl2,
      port: appConfig.dbPort2,
      logging: console.log,
      define:{
        raw:true
      }
    });
  }

  const SysUserService = appRequire('service/desktop/SysUserService.js');
  appConfig.allowedUrls = await SysUserService.findUserCommonAuthItem();
  console.log(appConfig.allowedUrls);
})();




